package com.escool.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.escool.model.TbGrado;
import com.escool.model.TbNivel;
import com.escool.model.TbRol;
import com.escool.service.GradosService;
import com.escool.service.NivelesService;
import com.escool.service.RolService;

@Controller
public class RolesController {
	
	@Autowired
	RolService rolService;

	
	@RequestMapping("showRoles")
	public String showRoles(Map<String, Object> map){
		map.put("tbRol", new TbRol());
		map.put("rolesList", rolService.listRol());
		return "/catRoles";
	}
	
	@RequestMapping(value = "addRoles", method = RequestMethod.POST)
	public String addRoles(@ModelAttribute("tbRol") @Valid TbRol tbRol, BindingResult result){
		if (!result.hasErrors()) {
			rolService.addRol(tbRol);
			return "redirect:/showRoles";
		}
		return "";
	}
	
	@RequestMapping(value = "editRoles", method = RequestMethod.POST)
	public String editRoles(@ModelAttribute("tbRol") @Valid TbRol tbRol, BindingResult result){
		if (!result.hasErrors()) {
			rolService.updateRol(tbRol);
			return "redirect:/showRoles";
		}
		return "";
	}
	
	@RequestMapping(value = "delRoles", method = RequestMethod.POST)
	public String delRoles(@ModelAttribute("tbRol") @Valid TbRol tbRol, BindingResult result){
		if (!result.hasErrors()) {
			rolService.delRol(tbRol.getIdRol());
			return "redirect:/showRoles";
		}
		return "";
	}

}
